//
//  AddEquipmentViewController.m
//  EPBPro
//
//  Created by wucha on 2018/7/31.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "AddEquipmentViewController.h"
#import "ResizeView.h"
#import "PlacePickerView.h"
#import "ChooseMapViewController.h"
#import "WCPickerView.h"
#import "HttpEquipmentAction.h"

@interface AddEquipmentViewController (){
    CLLocationCoordinate2D cllocationCoordinate2D;

}
@property (nonatomic,weak) UITextField *codeTF;
@property (nonatomic,weak) UITextField *nameTF;
@property (nonatomic,weak) UITextField *typeTF;
@property (nonatomic,weak) UITextField *costTF;
@property (nonatomic,weak) UIButton *cityButton;
@property (nonatomic,weak) UIButton *addressButton;
@property (nonatomic,weak) UIButton *installTimeButton;
@property (nonatomic,strong) NSString *cityString;
@property (nonatomic,strong) NSString *districtString;
@property (nonatomic,strong) NSString *province;
@end

@implementation AddEquipmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"添加机位";
    ResizeView *resizeView = [[ResizeView alloc] initWithFrame:CGRectMake(0, StatusBarHeight+44, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarHeight-44)];
    [self.view addSubview:resizeView];
    ResizeViewInfo *codeRV = [self getNormalResizeViewInfo:@"设备编码:"];
    self.codeTF = (UITextField*)[codeRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:codeRV];
    
    ResizeViewInfo *nameRV = [self getNormalResizeViewInfo:@"设备名称:"];
    self.nameTF = (UITextField*)[nameRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:nameRV];
    ResizeViewInfo *typeRV = [self getNormalResizeViewInfo:@"设备类型:"];
    self.typeTF = (UITextField*)[typeRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:typeRV];
    [resizeView addResizeSubview:[self getProvinceResizeViewInfo:@"省市区:" value:@""]];
    [resizeView addResizeSubview:[self getChoosePlaceResizeViewInfo:@"地址选择:" value:@""]];
    [resizeView addResizeSubview:[self getInstallTimeResizeViewInfo:@"安装时间:"]];
    ResizeViewInfo *costRV = [self getNormalResizeViewInfo:@"设备成本:"];
    self.costTF = (UITextField*)[costRV viewWithKey:@"TF"];
    
    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:30]];
    [resizeView addResizeSubview:[self getSubmitResizeViewInfo]];
    
}

-(ResizeViewInfo*)getSpaceResizeViewInfo:(int)height{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = height;
    
    return resizeViewInfo;

}

-(ResizeViewInfo*)getSubmitResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 40;
    
    UIButton *submitButton = [UIButton new];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [submitButton setTitle:@"提交" forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitButton.backgroundColor = DefaultTenantColor;
    [resizeViewInfo addSubview:submitButton];
    submitButton.sd_layout.leftSpaceToView(resizeViewInfo.view, 40).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-80);
    
    return resizeViewInfo;
}

-(void)submitAction:(id)sender{
    if([self.codeTF.text isEqualToString:@""]){
        [self showMessage:@"请输入设备编号" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.nameTF.text isEqualToString:@""]) {
        [self showMessage:@"请输入名称" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.typeTF.text isEqualToString:@""]) {
        [self showMessage:@"请输入设备类型" autoDissmissAfterDelay:0];
        return;
    }
    if (self.cityButton.titleLabel.text.length == 0) {
        [self showMessage:@"请选择省市区" autoDissmissAfterDelay:0];
        return;
    }
    
    if (self.addressButton.titleLabel.text.length == 0) {
        [self showMessage:@"请选择地址" autoDissmissAfterDelay:0];
        return;
    }
    if (self.installTimeButton.titleLabel.text.length == 0) {
        [self showMessage:@"请选择安装时间" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.costTF.text isEqualToString:@""]) {
        [self showMessage:@"请输入设备成本" autoDissmissAfterDelay:0];
        return;
    }
    AddEquipmentActionInfo *addEquipmentActionInfo = [[AddEquipmentActionInfo alloc] init];
    addEquipmentActionInfo.code = self.codeTF.text;
    addEquipmentActionInfo.name = self.nameTF.text;
    addEquipmentActionInfo.type = self.typeTF.text;
    addEquipmentActionInfo.city = [self.cityString isEqualToString:@"市辖区"]?self.province:self.cityString;
    addEquipmentActionInfo.district = self.districtString;
    addEquipmentActionInfo.status = @"open";
    addEquipmentActionInfo.buildDate = [NSString stringWithFormat:@"%@ 00:00:00",self.installTimeButton.titleLabel.text];
    addEquipmentActionInfo.latitude = [NSString stringWithFormat:@"%f",cllocationCoordinate2D.latitude];
    addEquipmentActionInfo.longitude = [NSString stringWithFormat:@"%f",cllocationCoordinate2D.longitude];
    addEquipmentActionInfo.cost = self.costTF.text;
    [self showWaitingViewWithMessage:@"提交中..." style:WaitingViewShowStyleFullSelfView];
    [HttpEquipmentAction postAddEquipmentWithAction:addEquipmentActionInfo block:^(id result, NSError *error) {
        [self dismissWaitingView];
        if (!error) {
            [self showMessage:@"添加成功" autoDissmissAfterDelay:0];
            [self performBlock:^{
                [self.navigationController popViewControllerAnimated:YES];
            } afterDelay:0.8];
        }
    }];
    
}

-(ResizeViewInfo*)getProvinceResizeViewInfo:(NSString*)title value:(NSString*)valueString{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UIButton *nameButton = [UIButton new];
    [nameButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    nameButton.titleLabel.font = [UIFont systemFontOfSize:16];
    nameButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [resizeViewInfo addSubview:nameButton];
    nameButton.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(200);
    [nameButton addTarget:self action:@selector(nameAction:) forControlEvents:UIControlEventTouchUpInside];
    self.cityButton = nameButton;
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = DefaultCellLineColor;
    [resizeViewInfo addSubview:lineView];
    lineView.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 0).heightIs(1);
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getChoosePlaceResizeViewInfo:(NSString*)title value:(NSString*)valueString{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UIButton *nameButton = [UIButton new];
    [nameButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    nameButton.titleLabel.font = [UIFont systemFontOfSize:16];
    nameButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [resizeViewInfo addSubview:nameButton];
    nameButton.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(200);
    [nameButton addTarget:self action:@selector(chooseAction:) forControlEvents:UIControlEventTouchUpInside];
    self.addressButton = nameButton;
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = DefaultCellLineColor;
    [resizeViewInfo addSubview:lineView];
    lineView.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 0).heightIs(1);
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getInstallTimeResizeViewInfo:(NSString*)title{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UIButton *nameButton = [UIButton new];
    [nameButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    nameButton.titleLabel.font = [UIFont systemFontOfSize:16];
    nameButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [resizeViewInfo addSubview:nameButton];
    nameButton.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(200);
    [nameButton addTarget:self action:@selector(chooseTimeAction:) forControlEvents:UIControlEventTouchUpInside];
    self.installTimeButton = nameButton;
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = DefaultCellLineColor;
    [resizeViewInfo addSubview:lineView];
    lineView.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 0).heightIs(1);
    
    return resizeViewInfo;
}

-(void)chooseTimeAction:(id)sender{
    UIButton *button = sender;
    WCPickerView *wcPickerView = [[WCPickerView alloc] initWithDataPick];
    [wcPickerView setWcDataPickSuccessWithBlock:^(NSString *string) {
        [button setTitle:string forState:UIControlStateNormal];
    }];
    [self.view addSubview:wcPickerView];
}

-(void)chooseAction:(id)sender{
    UIButton *button = sender;
    ChooseMapViewController *chooseMapViewController = [[ChooseMapViewController alloc] initWithChooseBlock:^(CLLocationCoordinate2D coordinate) {
        cllocationCoordinate2D = coordinate;
        [button setTitle:@"已选择" forState:UIControlStateNormal];
    }];
    [self.navigationController pushViewController:chooseMapViewController animated:YES];
}



-(void)nameAction:(id)sender{
    [self.view endEditing:YES];
    UIButton *button = sender;
    PlacePickerView *placePickerView = [[PlacePickerView alloc] initViewWithArray:^(NSString *province, NSString *city, NSString *area) {
        self.province = province;
        self.cityString = city;
        self.districtString = area;
        [button setTitle:[NSString stringWithFormat:@"%@%@%@",province,city,area] forState:UIControlStateNormal];
    }];
    [self.view addSubview:placePickerView];
}


-(ResizeViewInfo*)getNormalResizeViewInfo:(NSString*)title{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UITextField *nameTF = [UITextField new];
    nameTF.textColor = [UIColor grayColor];
    nameTF.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:nameTF viewKey:@"TF"];
    nameTF.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 10);
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = DefaultCellLineColor;
    [resizeViewInfo addSubview:lineView];
    lineView.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 0).heightIs(1);
    return resizeViewInfo;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
